package com.ticket.controller.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ticket.entity.ForumPost;
import com.ticket.entity.ForumComment;
import com.ticket.service.IForumService;
import com.ticket.utils.Result;
import com.ticket.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/forum")
public class ForumController {

    @Autowired
    private IForumService forumService;

    @GetMapping("/posts")
    public Result<Page<ForumPost>> getPosts(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        return Result.success(forumService.getPostPage(page, size));
    }

    @PostMapping("/posts")
    public Result<Long> createPost(@RequestBody ForumPost post) {
        Long userId = UserContext.getCurrentUserId();
        post.setUserId(userId);
        forumService.createPost(post);
        return Result.success(post.getId());
    }

    @GetMapping("/posts/{postId}")
    public Result<ForumPost> getPost(@PathVariable Long postId) {
        return Result.success(forumService.getPostDetail(postId));
    }

    @GetMapping("/posts/{postId}/comments")
    public Result<Page<ForumComment>> getComments(
            @PathVariable Long postId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        return Result.success(forumService.getCommentPage(postId, page, size));
    }

    @PostMapping("/posts/{postId}/comments")
    public Result<Long> createComment(
            @PathVariable Long postId,
            @RequestBody ForumComment comment) {
        Long userId = UserContext.getCurrentUserId();
        comment.setUserId(userId);
        comment.setPostId(postId);
        forumService.createComment(comment);
        return Result.success(comment.getId());
    }
} 